package com.esm.sm800.sm800floating;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.esm.sm800.R;

//Ftp设置
public class settting_upload extends Activity implements View.OnClickListener {
    private TextView setting_button_return; // 返回
    private TextView setting_button_upload_linktest; // 数据上传
    Switch switchButton;    // 数据上传开关
    private String server = "";
    private String user = "";
    private String pass = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 设置Activity为全屏
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_settting_upload);

        // 返回
        setting_button_return = (TextView) findViewById(R.id.setting_button_return);
        findViewById(R.id.setting_button_return).setOnClickListener(this);
        // 数据上传
        setting_button_upload_linktest = (TextView) findViewById(R.id.setting_upload_confirm);
        findViewById(R.id.setting_upload_confirm).setOnClickListener(this);
        // 是否有服务器开关
        switchButton = findViewById(R.id.setting_switch_button);
        findViewById(R.id.setting_switch_button).setOnClickListener(this);

        SharedPreferences prefs = getSharedPreferences(GlobalUtil.PREFS_NAME, Context.MODE_PRIVATE);
        ((EditText) findViewById(R.id.setting_upload_IP)).setText(prefs.getString(GlobalUtil.Key_ftpserverip, ""));
        ((EditText) findViewById(R.id.setting_upload_input_user)).setText(prefs.getString(GlobalUtil.Key_ftpserveruser, ""));
        ((EditText) findViewById(R.id.setting_upload_input_pass)).setText(prefs.getString(GlobalUtil.Key_ftpserverpass, ""));
        switchButton.setChecked(prefs.getBoolean(GlobalUtil.Key_hasdataupload_swtich, false));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.setting_button_return:
                this.finish();
                break;
            case R.id.setting_upload_confirm:
                // 确定
                // 保存语言设置到SharedPreferences
                server = ((EditText) findViewById(R.id.setting_upload_IP)).getText().toString().trim();
                user = ((EditText) findViewById(R.id.setting_upload_input_user)).getText().toString().trim();
                pass = ((EditText) findViewById(R.id.setting_upload_input_pass)).getText().toString().trim();

                SharedPreferences prefs1 = getSharedPreferences(GlobalUtil.PREFS_NAME, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor1 = prefs1.edit();
                editor1.putString(GlobalUtil.Key_ftpserverip, server);
                editor1.putString(GlobalUtil.Key_ftpserveruser, user);
                editor1.putString(GlobalUtil.Key_ftpserverpass, pass);
                editor1.apply();
                editor1.commit();

                startFtpUpload();

                Toast.makeText(this, getString(R.string.setting_upload_saveok), Toast.LENGTH_SHORT).show();
                break;
            case R.id.setting_switch_button:
                // 保存语言设置到SharedPreferences
                SharedPreferences prefs2 = getSharedPreferences(GlobalUtil.PREFS_NAME, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor2 = prefs2.edit();
                editor2.putBoolean(GlobalUtil.Key_hasdataupload_swtich, switchButton.isChecked());
                editor2.apply();
                editor2.commit();
                GlobalUtil.setDatauploadSwitch(switchButton.isChecked());
                break;
        }
    }

    private void startFtpUpload() {
        new Thread(() -> {
            if (server.isEmpty() || user.isEmpty() || pass.isEmpty()) {
                //LogFileManager.writeLog(getApplicationContext(), server+ ";" + user + ";" + pass);
                return;
            }
            // 在这里执行FTP上传操作
            new FtpUploadUtil(server, user, pass, "", "", true).execute();

            //LogFileManager.writeLog(getApplicationContext(), getString(R.string.setting_upload_link_success));
        }).start();
    }
}